<?php
namespace app\login\controller;
use app\login\model\Datalogin;
use \think\Session;

class Login extends\think\Controller {
	//显示登录界面
    public function crm_login() {
    	if(session("user_id")){
    		$this->redirect("home/index/index");
    	}else{
        return view();
    	}
    }

    //确认登录信息
    public function confirm_login() {
        $username = input("post.username");
        $password = input("post.password");
        $captcha = input("post.captcha");
        $Datalogin = model('Datalogin');



		$str = $Datalogin->where(['account'=>$username,'password'=>$password])->find();

		if(!captcha_check($captcha)){
			return json([
				"state"=>0,
				"msg"=>"验证码错误"
			]);
		}
		if(!empty($str)){
			$user_id = $str['id'];
			$user_name = $str['name'];
			$permissionsgroup_id = $str['permissionsgroup_id'];
            session('user_id',$user_id);
            session('user_name',$user_name);
            session('permissionsgroup_id',$permissionsgroup_id);
			return json([
				"state"=>1,
				"msg"=>""
			]);
		}else{
			return json([
				"state"=>0,
				"msg"=>"用户名和密码错误"
			]);
		}
    }
    //退出系统
    public function exit_crm(){
    	Session::clear();
    	return true;
    }
}